package com.liesp.base;

import java.util.List;


/**
* 项目名称：Liesp<br>
* 类名称：Pagination <br>  
* 类描述：  分页栏生成工具类,作参考 <br>
* 创建人：许彪   
* 创建时间：2009-8-28 上午11:29:11 <br> 
* 修改人：   
* 修改时间：                  <br>  
* 修改备注：   
* @version V1.0
*/
public class Pagination<T> {

	private int totalCount;//总条数
	private int totalPage;// 总页数
	private int currentPage;// 当前页
	private int pageSize = 20;
	private String url;
	
	public List<T> getData() {
		return data;
	}

	public void setData(List<T> data) {
		this.data = data;
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	private String pageString;// 分页字符串

	private List<T> data;

	public int getTotalPage() {
		if(totalCount % pageSize == 0) {
			totalPage = totalCount / pageSize;
		} else {
			totalPage = totalCount / pageSize + 1;
		}
		
		return totalPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentPage() {
		return currentPage == 0 ? 1 : currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getStartIndex() {
		return currentPage <= 1 ? 1 : currentPage * this.getPageSize() + 1;
	}

	public String getPageString() {
		String pagesinfo="";
		String fullurl="";
		int index=url.lastIndexOf("?");
		if(index>=0){fullurl=url+"&";}else {fullurl=url+"?";}
		if(this.totalCount == 0) {totalCount = 1;}
		
		if (this.getTotalPage() !=1 ) {      //1也不显示
			int start=1;
			if(this.getCurrentPage() > 3) {
				start = this.getCurrentPage()-3;
			}
			int last = this.getCurrentPage() + 3;
			if (this.getCurrentPage() + 3 > this.getTotalPage()) {
				last = this.getTotalPage();
			}
			
			if (this.getCurrentPage() > 1) {
				pagesinfo+="<a class=\"tiao_p\"  href=\""+fullurl+"pagination.currentPage="+1+"\">首页</a> <a href=\""+fullurl+"pages="+(this.getCurrentPage()-1)+"\"><</a>";}
				for(int i = start; i <= last; i++) {
					if(i == this.getCurrentPage()) {
						pagesinfo += "<span>" + this.getCurrentPage() + "</span>";
					} else {
					pagesinfo += "<a href=\"" + fullurl + "pagination.currentPage=" + i + "\">"
							+ i + "</a>";
					}
						
				}
			if(this.getCurrentPage()!= this.getTotalPage()) {
				pagesinfo += "<a href=\"" + fullurl + "pagination.currentPage=" + (this.getCurrentPage() + 1)
						+ "\">></a> <a class=\"tiao_p\" href=\"" + fullurl
						+ "pagination.currentPage=" + this.getTotalPage() + "\">末页</a>";
			}
		}
		
		return pageString;
	}
	
}
